#include <math.h>
#include "d_Math.h"

using namespace d_Math;

/////////////d_Vec3f helper funcs//////////////////
float d_Math::len(const d_Vec3f a)
{
	return sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
}

d_Vec3f d_Math::norm(const d_Vec3f a)
{
	float length = len((const d_Vec3f) a);
	return d_Vec3f(a[0]/length, a[1]/length, a[2]/length);
}

float d_Math::dot(const d_Vec3f a, const d_Vec3f b)
{
	return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}

d_Vec3f d_Math::cross(const d_Vec3f a, const d_Vec3f b)
{
    return d_Vec3f(a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]);
}


///////////d_Vec4f helper funcs/////////////
float d_Math::len(const d_Vec4f a)
{
	return sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2] + a[3]*a[3]);
}

d_Vec4f d_Math::norm(const d_Vec4f a)
{
	float length = len(a);
	return d_Vec4f(a[0]/length, a[1]/length, a[2]/length, a[3]/length);
}

float d_Math::dot(const d_Vec4f a, const d_Vec4f b)
{
	return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
}

/////////////d_Mat4f helper funcs///////////////
d_Mat4f d_Math::trans(const d_Mat4f &m)
{
#define M(x,y) m(x,y)
#define R(x,y) result(x,y)

	d_Mat4f result;

	R(0,0) = M(0,0); R(0,1) = M(1,0); R(0,2) = M(2,0); R(0,3) = M(3,0);
	R(1,0) = M(0,1); R(1,1) = M(1,1); R(1,2) = M(2,1); R(1,3) = M(3,1);
	R(2,0) = M(0,2); R(2,1) = M(1,2); R(2,2) = M(2,2); R(2,3) = M(3,2);
	R(3,0) = M(0,3); R(3,1) = M(1,3); R(3,2) = M(2,3); R(3,3) = M(3,3);
		
	return(result);
	
#undef M
#undef R
}

